import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.File;
import java.util.Vector;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.GroupLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import javax.swing.WindowConstants;
import javax.swing.SwingUtilities;
import Cluedo.GUI2.*;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class CluemeLauncher extends javax.swing.JFrame implements ActionListener, ItemListener {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel imagePanel;
	private JLabel mainScreenImage;
	private JPanel cardPanel;
	private JPanel mainMenuPanel;
	private JPanel startGamePanel;
	private JCheckBox chooserCheckBox[];
	private JComboBox chooserComboBox[];
	private JTextField characterLabel[];
	private String[] characterArray = {"Miss Scarlet", "Mrs. White", "Mrs. Peacock", "Prof. Plum", "Col. Mustard", "Mr. Green"};
	private int PLAYERSELECTED;
	final static String MAINSCREENCARD = "main screen";
	final static String STARTGAMECARD = "start game";
	

	{
		//Set Look & Feel
		try {
			javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName());
		} catch(Exception e) {
			e.printStackTrace();
		}
	}


	/**
	* Auto-generated main method to display this JFrame
	*/
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				CluemeLauncher inst = new CluemeLauncher();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}
	
	public CluemeLauncher() {
		super("Clume");
		initGUI();
	}
	
	private void initGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			pack();
			setSize(400, 600);
			BorderLayout thisLayout = new BorderLayout();
			getContentPane().setLayout(thisLayout);
			setResizable(false);
			{ // SET UP THE IMAGE PANEL
				imagePanel = new JPanel();
				FlowLayout imagePanelLayout = new FlowLayout();
				imagePanelLayout.setHgap(0);
				imagePanelLayout.setVgap(0);
				
				imagePanel.setLayout(imagePanelLayout);
				
				ImageIcon fillerImageIcon = new ImageIcon("images/main.jpg");
				mainScreenImage = new JLabel();
				mainScreenImage.setIcon(fillerImageIcon);
				
				imagePanel.add(mainScreenImage);
				getContentPane().add(imagePanel, BorderLayout.PAGE_START);
				
			}
			{ // SET UP THE MAIN MENU PANEL
				mainMenuPanel = new JPanel();
				mainMenuPanel.setLayout(new BoxLayout(mainMenuPanel, BoxLayout.Y_AXIS));
				
				mainMenuPanel.setAlignmentX(CENTER_ALIGNMENT);
				mainMenuPanel.setLayout(new BoxLayout(mainMenuPanel, BoxLayout.Y_AXIS));
				
				JButton menuScreenButton[] = new JButton[3];
				
				menuScreenButton[0] = new JButton("Start Editor");
				menuScreenButton[0].setActionCommand("starteditor");
				menuScreenButton[0].setToolTipText("Click this button to start Clueme Editor.");
				
				menuScreenButton[1] = new JButton("Start Game");
				menuScreenButton[1].setActionCommand("startgame");
				menuScreenButton[1].setToolTipText("Click this button to start the game Clue with AI files loaded.");
				
				menuScreenButton[2] = new JButton("Exit");
				menuScreenButton[2].setActionCommand("exit");
				menuScreenButton[2].setToolTipText("Click this button to exit Clueme");
				
				Box buttonBox = Box.createVerticalBox();
				
				
				
				for(int i = 0; i < menuScreenButton.length; i++) {
					menuScreenButton[i].addActionListener(this);
					
					Box row = Box.createHorizontalBox();
					
					menuScreenButton[i].setMaximumSize(new Dimension(150, 50));
					row.add(menuScreenButton[i]);

					row.add( Box.createVerticalGlue() );
					
					buttonBox.add(row);
				}
				
				mainMenuPanel.add(buttonBox);
				
//				getContentPane().add(mainMenuPanel, BorderLayout.CENTER);
				
			}
			{ // SET UP THE START GAME PANEL
				startGamePanel = new JPanel(new BorderLayout());
				JPanel startTextPanel = new JPanel();
				startTextPanel.setLayout(new BoxLayout(startTextPanel, BoxLayout.Y_AXIS));
				JLabel blankLabel[] = new JLabel[10];
				JLabel instructionLabel = new JLabel ("                                   Select at least three AI players to load");
				
				for (int i = 0; i < 2; i++){
					blankLabel[i] = new JLabel(" ");
					startTextPanel.add(blankLabel[i]);
				}

				startTextPanel.add(instructionLabel);

				for (int i = 2; i < 3; i++){
					blankLabel[i] = new JLabel(" ");
					startTextPanel.add(blankLabel[i]);
				}
				
				startGamePanel.add(startTextPanel, BorderLayout.NORTH);
				
//				JPanel optionsPanel = new JPanel();
//				GroupLayout optionsLayout = new GroupLayout(optionsPanel);
//				optionsLayout.setAutoCreateGaps(true);
//				optionsLayout.setAutoCreateContainerGaps(true);
				
				JPanel checkcomboPanel = new JPanel(); //holder for checkboxes and comboboxes
				JPanel checkboxPanel = new JPanel(); //holder for all checkboxes
				JPanel comboboxPanel = new JPanel(); //holder for all comboboxes
				JPanel characterLabelPanel = new JPanel();
				checkboxPanel.setLayout(new BoxLayout(checkboxPanel, BoxLayout.Y_AXIS));
				comboboxPanel.setLayout(new BoxLayout(comboboxPanel, BoxLayout.Y_AXIS));
				characterLabelPanel.setLayout(new BoxLayout(characterLabelPanel, BoxLayout.Y_AXIS));
				
				
				
				chooserCheckBox = new JCheckBox[6];
				
				for (int i = 0; i < 6; i++){
					chooserCheckBox[i] = new JCheckBox();
					chooserCheckBox[i].setEnabled(true);
					checkboxPanel.add(chooserCheckBox[i]);
					chooserCheckBox[i].addItemListener(this);
				}
				
				 
				Vector<String> options =  getListOfFiles(".", ".java");
				
				options.add(0, "Select AI file to load            ");
				
				chooserComboBox = new JComboBox[6];
				
				for (int i = 0; i < 6; i++){
					chooserComboBox[i] = new JComboBox(options);
					chooserComboBox[i].setEnabled(false);
					comboboxPanel.add(chooserComboBox[i]);
				}
				
				characterLabel = new JTextField[6];
				
				
				for (int i = 0; i < 6; i++){
					characterLabel[i] = new JTextField(characterArray[i]);
					characterLabel[i].setEditable(false);
//					characterLabel[i].setEnabled(false);
					characterLabelPanel.add(characterLabel[i]);
				}
				
				
				
				
				checkcomboPanel.add(checkboxPanel);
				checkcomboPanel.add(comboboxPanel);
				checkcomboPanel.add(characterLabelPanel);
				
				startGamePanel.add(checkcomboPanel, BorderLayout.CENTER);
				
				JPanel bottomButtons = new JPanel();
				bottomButtons.setAlignmentX(RIGHT_ALIGNMENT);
				
				JButton startGameButton = new JButton("Start Cluedo");
				startGameButton.setActionCommand("startgame2");
				startGameButton.addActionListener(this);
				
				JButton backMainButton = new JButton("Main Menu");
				backMainButton.setActionCommand("mainmenu");
				backMainButton.addActionListener(this);
				
				bottomButtons.add(backMainButton);
				bottomButtons.add(startGameButton);

				
				startGamePanel.add(bottomButtons, BorderLayout.SOUTH);				
			}
			
			
			{ // SET UP THE CARD PANEL 
				cardPanel = new JPanel(new CardLayout());
				cardPanel.add(startGamePanel, STARTGAMECARD);
				cardPanel.add(mainMenuPanel, MAINSCREENCARD);
				getContentPane().add(cardPanel, BorderLayout.CENTER);
			    CardLayout cl = (CardLayout)(cardPanel.getLayout());
			    cl.show(cardPanel, MAINSCREENCARD);
			}
			this.setFocusCycleRoot(false);
		} catch (Exception e) {
		    //add your error handling code here
			e.printStackTrace();
		}
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub
		System.out.println("The action was " + arg0.getActionCommand());
		CardLayout c2 = (CardLayout)(cardPanel.getLayout());
		
		
		if ("startgame".equals(arg0.getActionCommand())){
			PLAYERSELECTED = 0;
			//c2.show(cardPanel, STARTGAMECARD);
			GameMenu2.getConfig();
			if(GameMenu2.properties.get("decoration") != null && GameMenu2.properties.get("decoration").equals("true") ) {
				Cluedo.API.Resources.configureUI();
			}
			if(GameMenu2.properties.get("sound") != null && GameMenu2.properties.get("sound").equals("true") ) {
			    GameMenu2.soundEnabled = true;
			}
			else
			    GameMenu2.soundEnabled = false;
			
			GameMenu2 welcome = new GameMenu2("Cluedo");
			
			welcome.setVisible(true);
			
		}
//		if ("startgame2".equals(arg0.getActionCommand())){
//			if(PLAYERSELECTED < 3) {
//			    JOptionPane.showMessageDialog(null, "Cannot start game because the number of players is not enough. " +
//			    									"\nSelect at least three players.", "Clueme", JOptionPane.ERROR_MESSAGE);
//			    return;
//			}
//		}
		if ("starteditor".equals(arg0.getActionCommand())){
			// RUN EDITOR
		}
		if ("exit".equals(arg0.getActionCommand())){
			// QUIT CLUEME
			System.exit(0);
		}
		if  ("mainmenu".equals(arg0.getActionCommand())){
			c2.show(cardPanel, MAINSCREENCARD);
		}
		
	}

	@Override
	public void itemStateChanged(ItemEvent e) {
		// TODO Auto-generated method stub
		// FOR CHECKBOXES for START GAME CARD PANEL
		Object source = e.getItemSelectable();

		for (int i = 0; i < 6; i++){
			if (source == chooserCheckBox[i]){
				if (e.getStateChange()== ItemEvent.SELECTED){
					chooserComboBox[i].setEnabled(true);
					System.out.println("player selected is " + ++PLAYERSELECTED);
				}
				else if (e.getStateChange()== ItemEvent.DESELECTED){ 
					chooserComboBox[i].setEnabled(false);
					System.out.println("player selected is " + --PLAYERSELECTED);
				}
			}
		}
		
		for (int i = 0; i < 6; i++){
			if (source == characterLabel[i]){
				if (e.getStateChange()== ItemEvent.SELECTED)
					characterLabel[i].setEnabled(true);
				else if (e.getStateChange()== ItemEvent.DESELECTED) 
					characterLabel[i].setEnabled(false);
			}
		}
		
	}
	
	private Vector<String> getListOfFiles(String folderPath, String extension){ 
		 String filename;
		 File folder = new File(folderPath);
		 File[] listOfFiles = folder.listFiles();
		 Vector<String> files = new Vector<String>();
		 
	 
		 for (int i = 0; i < listOfFiles.length; i++) {
			 if (listOfFiles[i].isFile()){
				 filename = listOfFiles[i].getName();
				 
				 if (filename.endsWith(extension)){
					 files.add(filename);
				 }
			 }
		 }
		
		return files;
	}

}
